package com.situ.springboot.interceptor;

import com.situ.springboot.pojo.entity.Admin;
import com.situ.springboot.pojo.entity.Custom;
import com.situ.springboot.pojo.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断有没有登录
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Admin admin = (Admin) session.getAttribute("admin");
        Custom custom = (Custom) session.getAttribute("custom");
        if (user == null && admin == null && custom == null) {
            response.sendRedirect("/user/toLogin");
            return false;
        }

        //之前已经登录过，放行，访问后面的Controller
        return true;
    }
}
